#ifndef __ID2OBJ_H__
#define __ID2OBJ_H__
#include <stdio.h>


template <typename T>
class ID2Obj
{
    public:
        ID2Obj() {
            m_nMaxFD = -1;
            m_pObjs = NULL;
        }

        ~ID2Obj(){
            m_nMaxFD = -1;
            delete [] m_pObjs;
        }

        bool Init(short nMaxID){
            m_nMaxFD = nMaxID;
            m_pObjs = new T*[m_nMaxFD + 1];
            if (!m_pObjs){
                return false;
            }
            for(int i=0; i<= m_nMaxFD; i++){
                m_pObjs[i] = NULL;
            }
            return true;
        }

        bool Add(short nID, T* pObj){
            if (nID >=0 && nID <= m_nMaxFD){
                m_pObjs[nID] = pObj;
            }
            else{
                return false;
            }
        }

        bool Del(short nID){
            if (nID >=0 && nID <= m_nMaxFD){
                m_pObjs[nID] = NULL;
                return true;
            }
            else{
                fprintf(stderr, "Invalid id:%d\n", nID);
                return false;
            }
        }

        T* Get(short nID){
            if (nID >=0 && nID <= m_nMaxFD){
                return m_pObjs[nID];
            }
            else{
                return NULL;
            }

        }

    private:
        short m_nMaxFD;
        T** m_pObjs;
};


#endif //__ID2OBJ_H__
